home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / pop3cli.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  150 lines

  1. /* Post Office Protocol (POP3) Client -- RFC1225
  2.  * Copyright 1992 William Allen Simpson
  3.  *      partly based on a NNTP client design by Anders Klemets, SM0RGV
  4.  *      and POP2 Client by Mike Stockett, WA7DYX, et alia.
  5.  */
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include "global.h"
  9. #include "timer.h"
  10. #include "proc.h"
  11. #include "netuser.h"
  12. #include "socket.h"
  13. #include "cmdparse.h"
  14. #include "files.h"
  15. #include "mailcli.h"
  16. #include "mailutil.h"
  17. #include "smtp.h"
  18.  
  19. #ifdef POP3CLIENT
  20.  
  21. void
  22. pop3_job(unused,v1,p2)
  23. int unused;
  24. void *v1;
  25. void *p2;
  26. {
  27.     struct mailservers *np = v1;
  28.     struct sockaddr_in fsocket;
  29.     char buf[TLINELEN];
  30.     char *cp;
  31.     FILE *wfp = NULLFILE;
  32.     time_t t;
  33.     int s = -1;
  34.     int bytes;
  35.     int messages;
  36.     int i;
  37.  
  38.     if ( mailbusy( np ) )
  39.         return;
  40.  
  41.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  42.         /* No IP address found */
  43.         if (Mailtrace >= 1)
  44.             log(-1,"POP3 can't resolve host '%s'", np->hostname);
  45.         start_timer(&np->timer);
  46.         return;
  47.     }
  48.  
  49.     fsocket.sin_family = AF_INET;
  50.     fsocket.sin_port = IPPORT_POP3;
  51.  
  52.     s = socket(AF_INET,SOCK_STREAM,0);
  53.     sockmode(s,SOCK_ASCII);
  54.  
  55.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  56.         cp = sockerr(s);
  57.         if (Mailtrace >= 2)
  58.             log(s,"POP3 Connect failed: %s",
  59.                 cp != NULLCHAR ? cp : "" );
  60.         goto quit;
  61.     }
  62.  
  63.     log(s,"POP3 Connected to mailhost %s", np->hostname);
  64.  
  65.     /* Eat the banner */
  66.     if ( mailresponse( s, buf, "banner" ) == -1
  67.       || buf[0] == '-' ) {
  68.         goto quit;
  69.     }
  70.  
  71.     usprintf(s,"USER %s\n", np->username);
  72.     if ( mailresponse( s, buf, "USER" ) == -1
  73.       || buf[0] == '-' ) {
  74.         goto quit;
  75.     }
  76.  
  77.     usprintf(s,"PASS %s\n", np->password);
  78.     if ( mailresponse( s, buf, "PASS" ) == -1
  79.       || buf[0] == '-' ) {
  80.         goto quit;
  81.     }
  82.  
  83.     usprintf(s,"STAT\n" );
  84.     if ( mailresponse( s, buf, "STAT" ) == -1
  85.       || buf[0] == '-' ) {
  86.         goto quit;
  87.     }
  88.  
  89.     rip(buf);
  90.     if ( Mailtrace >= 1 )
  91.         log(s,"POP3 status %s",buf);
  92.     sscanf(buf,"+OK %d %d",&messages,&bytes);
  93.  
  94.     if ((wfp = tmpfile()) == NULLFILE) {
  95.         if ( Mailtrace >= 1 )
  96.             log(s,"POP3 Cannot create %s", "tmp file" );
  97.         goto quit;
  98.     }
  99.  
  100.     for ( i = 0; i++ < messages; ) {
  101.         usprintf(s,"RETR %d\n", i);
  102.         if ( mailresponse( s, buf, "RETR" ) == -1 )
  103.             goto quit;
  104.         if ( buf[0] == '-' ) {
  105.             continue;
  106.         }
  107.  
  108.         time(&t);
  109.         fprintf( wfp, "From POP3@%s %s",
  110.             np->hostname,
  111.             ctime(&t));
  112.  
  113.         if ( recvmail(s, buf, TLINELEN, wfp, Mailtrace) == -1 ) {
  114.             goto quit;
  115.         }
  116.  
  117.         usprintf(s,"DELE %d\n", i);
  118.         if ( mailresponse( s, buf, "DELE" ) == -1
  119.           || buf[0] == '-' ) {
  120.             goto quit;
  121.         }
  122.     }
  123.  
  124.     if ( messages == 0 ) {
  125.         /* Quit for politeness sake */
  126.         usprintf(s,"QUIT\n" );
  127.         mailresponse( s, buf, "QUIT" );
  128.     } else if ( copymail( Mailspool, np->mailbox,
  129.             buf, TLINELEN, wfp, Mailtrace ) != -1 ) {
  130.         /* Quit command allows the deletions to complete */
  131.         usprintf(s,"QUIT\n" );
  132.         mailresponse( s, buf, "QUIT" );
  133.  
  134.         tprintf("New mail arrived for %s from mailhost %s%c\n",
  135.                 np->mailbox,
  136.                 np->hostname,
  137.                 Mailquiet ? ' ' : '\007');
  138.     }
  139.  
  140. quit:
  141.     log(s,"POP3 daemon exiting" );
  142.     close_s(s);
  143.  
  144.     if (wfp != NULLFILE)
  145.         fclose(wfp);
  146.     start_timer(&np->timer);
  147. }
  148.  
  149. #endif
  150.